package com.aurora.oa.work.view;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.TextView;

import com.aurora.oa.Constants.AuroraConstants;
import com.aurora.oa.R;
import com.aurora.oa.activity.BaseActivity;
import com.aurora.oa.bean.HttpResult;
import com.aurora.oa.listener.OnRequestHttpDataListener;
import com.aurora.oa.model.RequestHttpModel;
import com.aurora.oa.model.RequestHttpModelImpl;
import com.aurora.oa.utils.ToastUtil;
import com.codbking.calendar.CalendarUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.DatePicker;
import cn.qqtheme.framework.picker.DateTimePicker;

/**
 * Created by Administrator on 2017/4/26 0026.
 */
public class AttendanceStatisticsActivity extends BaseActivity {

    DatePicker datePicker;
    @Bind(R.id.my_image_view)
    SimpleDraweeView myImageView;
    @Bind(R.id.tv_title)
    TextView tvName;
    @Bind(R.id.tv_postname)
    TextView tvPostname;
    @Bind(R.id.tv_date)
    TextView tvDate;
    @Bind(R.id.tv_attendance_number)
    TextView tvAttendanceNumber;
    @Bind(R.id.tv_tiaoxiu_number)
    TextView tvTiaoxiuNumber;
    @Bind(R.id.tv_leave_time)
    TextView tvLeaveTime;
    @Bind(R.id.tv_later_number)
    TextView tvLaterNumber;
    RequestHttpModel model = new RequestHttpModelImpl();
    String id;
    @Bind(R.id.tv_overtime_day)
    TextView tvOvertimeDay;
    private String currentDate;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitlebar(true, "出勤统计", true);
        setMyContentView(R.layout.activity_attendance_statistics);
        ButterKnife.bind(this);
        id = getIntent().getStringExtra("id");
        initView();
        getData();

    }

    private void getData() {
        model.getUserMonthData(id, currentDate, new OnRequestHttpDataListener() {
            @Override
            public void onRequestDataSuccess(String body) {
                Log.e("month_data",body);
                Gson gson = new Gson();
                Type type = new TypeToken<HttpResult<UIData>>() {
                }.getType();
                HttpResult<UIData> dataHttpResult = gson.fromJson(body, type);
                loadData(dataHttpResult.obj);
            }

            @Override
            public void onRequestDataFail(String message) {
                ToastUtil.show(getApplicationContext(),message);
            }
        });
    }

    private void loadData(UIData data) {
        myImageView.setImageURI(AuroraConstants.BASE_SERVER_URL+"/"+data.headImg);
        tvName.setText(data.userRealName);
        tvPostname.setText(data.postName);
        tvAttendanceNumber.setText(data.signNum);
        tvTiaoxiuNumber.setText(data.tiaoxiuNum);
        tvLeaveTime.setText(data.leavesNum);
        tvLaterNumber.setText(data.lateNum);
        tvOvertimeDay.setText(data.overtimeNum);
    }

    private void initView() {
        datePicker = new DatePicker(this, DateTimePicker.YEAR_MONTH);
        datePicker.setOnDatePickListener(new DatePicker.OnYearMonthPickListener() {
            @Override
            public void onDatePicked(String year, String month) {
                currentDate = year + "-" + month +"-" +"01";
                getData();
            }
        });

        int[] data = CalendarUtil.getYMD(new Date());

        //月份日期补0，服务器需要
        String month = data[1] + "";
        if (month.length() == 1) {
            month = "0" + month;
        }
        tvDate.setText(data[0] + "年" + month + "月");
        String day = data[2] + "";
        if (day.length() == 1) {
            day = "0" + day;
        }

        currentDate = data[0] + "-" + month + "-" + day;
    }

    @OnClick(R.id.tv_date)
    public void onClick() {
        datePicker.show();
    }

    class UIData {
        //        "signNum": 0,
//                "headImg": "attachment/image/815c2802cff34d7292204d7af532aa45/20170411/files1491893757.png",
//                "userRealName": "weir",
//                "postName": "系统管理员,经理",
//                "tiaoxiuNum": 0,
//                "leavesNum": 0,
//                "lateNum": 0,
//                "overtimeNum": 0
        public String signNum; //出勤天数
        public String tiaoxiuNum;//调休天数
        public String leavesNum;//请假次数
        public String headImg;
        public String postName;
        public String userRealName;
        public String lateNum;//迟到次数
        public String overtimeNum;//加班天数
    }

}
